﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web.Areas.SystemManage.Controllers
{
    public class DataItemDetailController : MvcControllerBase
    {
        private DataItemDetailBLL dataItemDetailBLL = new DataItemDetailBLL();
        private DataItemCache dataItemCache = new DataItemCache();

        #region 视图功能
        /// <summary>
        /// 明细管理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 明细表单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Form()
        {
            return View();
        }
        /// <summary>
        /// 明细详细
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Detail()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 明细列表 
        /// </summary>
        /// <param name="keyword">关键字查询</param>
        /// <returns>返回树形Json</returns>
        [HttpGet]
        public ActionResult GetTreeJson(string itemid)
        {
            var data = dataItemDetailBLL.GetList(itemid).ToList();
            var treeList = new List<TreeEntity>();
            foreach (DataItemDetailEntity item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.ItemDetailId) == 0 ? false : true;
                tree.id = item.ItemDetailId;
                tree.text = item.ItemName;
                tree.value = item.ItemCode;
                tree.parentId = item.ParentId;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }
        /// <summary>
        /// 明细列表
        /// </summary>
        /// <param name="itemId">分类Id</param>
        /// <param name="keyword">关键字查询</param>
        /// <returns>返回树形列表Json</returns>
        [HttpGet]
        public ActionResult GetTreeListJson(string itemId, string condition, string keyword)
        {
            var data = dataItemDetailBLL.GetList(itemId).ToList();
            if (!string.IsNullOrEmpty(keyword))
            {
                #region 多条件查询
                switch (condition)
                {
                    case "ItemName":        //项目名
                        data = data.TreeWhere(t => t.ItemName.Contains(keyword), "ItemDetailId");
                        break;
                    case "ItemValue":      //项目值
                        data = data.TreeWhere(t => t.ItemValue.Contains(keyword), "ItemDetailId");
                        break;
                    case "SimpleSpelling": //拼音
                        data = data.TreeWhere(t => t.SimpleSpelling.Contains(keyword), "ItemDetailId");
                        break;
                    default:
                        break;
                }
                #endregion
            }
            var TreeList = new List<TreeGridEntity>();
            foreach (DataItemDetailEntity item in data)
            {
                TreeGridEntity tree = new TreeGridEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.ItemDetailId) == 0 ? false : true;
                tree.id = item.ItemDetailId;
                tree.parentId = item.ParentId;
                tree.expanded = true;
                tree.hasChildren = hasChildren;
                tree.entityJson = item.ToJson();
                TreeList.Add(tree);
            }
            return Content(TreeList.TreeJson());
        }

        /// <summary>
        /// 明细列表 
        /// </summary>
        /// <param name="keyword">关键字查询</param>
        /// <returns>返回树形Json</returns>
        [HttpGet]
        public ActionResult GetParentTreeJson(string itemId, string parentId)
        {
            var data = dataItemDetailBLL.GetList(itemId, parentId).ToList();
            var treeList = new List<TreeEntity>();
            foreach (DataItemDetailEntity item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.ItemDetailId) == 0 ? false : true;
                tree.id = item.ItemDetailId;
                tree.text = item.ItemName;
                tree.value = item.ItemCode;
                if (data.Count(t => t.ItemDetailId == item.ParentId) == 0)
                { tree.parentId = "0"; }
                else
                {
                    tree.parentId = item.ParentId;
                }
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }

        /// <summary>
        /// 查询上级明细列表
        /// </summary>
        /// <param name="itemId">分类Id</param>
        /// <param name="keyword">关键字查询</param>
        /// <returns>返回树形列表Json</returns>
        [HttpGet]
        public ActionResult GetParentTreeListJson(string itemId, string parentId, string condition, string keyword)
        {
            var data = dataItemDetailBLL.GetList(itemId, parentId);
            if (!string.IsNullOrEmpty(keyword))
            {
                #region 多条件查询
                switch (condition)
                {
                    case "ItemName":        //项目名
                        data = data.Where(t => t.ItemName.Contains(keyword));
                        break;
                    case "ItemValue":      //项目值
                        data = data.Where(t => t.ItemValue.Contains(keyword));
                        break;
                    case "SimpleSpelling": //拼音
                        data = data.Where(t => t.SimpleSpelling.Contains(keyword));
                        break;
                    default:
                        break;
                }
                #endregion
            }
            var TreeList = new List<TreeGridEntity>();
            foreach (DataItemDetailEntity item in data)
            {
                TreeGridEntity tree = new TreeGridEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.ItemDetailId) == 0 ? false : true;
                tree.id = item.ItemDetailId;
                if (data.Count(t => t.ItemDetailId == item.ParentId) == 0)
                { tree.parentId = "0"; }
                else
                {
                    tree.parentId = item.ParentId;
                }
                tree.expanded = true;
                tree.hasChildren = hasChildren;
                tree.entityJson = item.ToJson();
                TreeList.Add(tree);
            }
            return Content(TreeList.TreeJson());
        }


        /// <summary>
        /// 明显列表带所有
        /// </summary>
        /// <param name="itemId">分类Id</param>
        /// <param name="keyword">关键字查询</param>
        /// <returns>返回树形列表Json</returns>
        [HttpGet]
        public ActionResult GetParentTreeListAllJson(string itemId, string parentId)
        {
            var data = dataItemDetailBLL.GetList(itemId, parentId).ToList();
            var treeList = new List<TreeEntity>();
            foreach (DataItemDetailEntity item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.ItemDetailId) == 0 ? false : true;
                tree.id = item.ItemDetailId;
                tree.text = item.ItemName;
                tree.value = item.ItemCode;
                if (data.Count(t => t.ItemDetailId == item.ParentId) == 0)
                { tree.parentId = ""; }
                else
                {
                    tree.parentId = item.ParentId;
                }
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                treeList.Add(tree);
            }
            TreeEntity tree1 = new TreeEntity();
            tree1.id = "";
            tree1.text = "所有";
            tree1.value = "";
            tree1.isexpand = true;
            tree1.complete = true;
            tree1.hasChildren = true;
            tree1.parentId = "0";
            treeList.Add(tree1);
            return Content(treeList.TreeToJson());
        }

        /// <summary>
        /// 明显列表带所有
        /// </summary>
        /// <param name="itemId">分类Id</param>
        /// <param name="keyword">关键字查询</param>
        /// <returns>返回树形列表Json</returns>
        [HttpGet]
        public ActionResult GetTreeListAllJson(string parentId)
        {
            var data = dataItemDetailBLL.GetList(parentId).ToList();
            var treeList = new List<TreeEntity>();
            foreach (DataItemDetailEntity item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.ItemDetailId) == 0 ? false : true;
                tree.id = item.ItemDetailId;
                tree.text = item.ItemName;
                tree.value = item.ItemCode;
                tree.parentId = item.ParentId == "0" ? "" : item.ParentId;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                treeList.Add(tree);
            }
            TreeEntity tree1 = new TreeEntity();
            tree1.id = "";
            tree1.text = "所有";
            tree1.value = "";
            tree1.isexpand = true;
            tree1.complete = true;
            tree1.hasChildren = true;
            tree1.parentId = "0";
            treeList.Add(tree1);
            return Content(treeList.TreeToJson());
        }

        /// <summary>
        /// 明细实体 
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns>返回对象Json</returns>
        [HttpGet]
        public ActionResult GetForm(string keyValue)
        {
            var data = dataItemDetailBLL.GetEntity(keyValue);
            return Content(data.ToJson());
        }

        /// <summary>
        /// 明细实体 
        /// </summary>
        /// <param name="ItemId"></param>
        /// <param name="ItemName"></param>
        /// <returns>返回对象Json</returns>
        [HttpGet]
        public ActionResult GetFormByItemIdJson(string ItemId, string ItemName)
        {
            var data = dataItemDetailBLL.GetEntity("ItemName", ItemName, "ItemId", ItemId);
            return Content(data.ToJson());
        }
        /// <summary>
        /// 获取数据字典列表（绑定控件）
        /// </summary>
        /// <param name="EnCode">代码</param>
        /// <returns>返回列表树Json</returns>
        [HttpGet]
        public ActionResult GetDataItemTreeJson(string EnCode)
        {
            var data = dataItemCache.GetDataItemList(EnCode);
            var treeList = new List<TreeEntity>();
            foreach (DataItemModel item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.ItemDetailId) == 0 ? false : true;
                tree.id = item.ItemDetailId;
                tree.text = item.ItemName;
                tree.value = item.ItemValue;
                tree.title = item.SimpleSpelling;
                tree.parentId = item.ParentId;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }
        /// <summary>
        /// 获取数据字典列表（绑定控件）
        /// </summary>
        /// <param name="EnCode">代码</param>
        /// <returns>返回列表Json</returns>
        [HttpGet]
        public ActionResult GetDataItemListJson(string EnCode)
        {
            var data = dataItemCache.GetDataItemList(EnCode);
            return Content(data.ToJson());
        }
        /// <summary>
        /// 获取数据字典子列表（绑定控件）
        /// </summary>
        /// <param name="EnCode">代码</param>
        /// <param name="ItemValue">项目值</param>
        /// <returns>返回列表Json</returns>
        public ActionResult GetSubDataItemListJson(string EnCode, string ItemValue)
        {
            var data = dataItemCache.GetSubDataItemList(EnCode, ItemValue);
            return Content(data.ToJson());
        }
        #endregion

        #region 验证数据
        /// <summary>
        /// 项目值不能重复
        /// </summary>
        /// <param name="ItemValue">项目值</param>
        /// <param name="keyValue">主键</param>
        /// <param name="itemId">分类Id</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ExistItemValue(string ItemValue, string keyValue, string itemId)
        {
            bool IsOk = dataItemDetailBLL.ExistItemValue(ItemValue, keyValue, itemId);
            return Content(IsOk.ToString());
        }
        /// <summary>
        /// 项目名不能重复
        /// </summary>
        /// <param name="ItemName">项目名</param>
        /// <param name="keyValue">主键</param>
        /// <param name="itemId">分类Id</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ExistItemName(string ItemName, string keyValue, string itemId)
        {
            bool IsOk = dataItemDetailBLL.ExistItemName(ItemName, keyValue, itemId);
            return Content(IsOk.ToString());
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 删除明细
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult RemoveForm(string keyValue)
        {
            try
            {
                dataItemDetailBLL.RemoveForm(keyValue);
                return Success("删除成功。");
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }
        }
        /// <summary>
        /// 保存明细表单（新增、修改）
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <param name="dataItemDetailEntity">明细实体</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, DataItemDetailEntity dataItemDetailEntity)
        {

            try
            {
                dataItemDetailBLL.SaveForm(keyValue, dataItemDetailEntity);
                return Success("操作成功。");
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }
        }
        #endregion


        #region setting
        /// <summary>
        /// 获取词典父子列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllTreeListJson()
        {
            var ItemData = new DataItemBLL().GetList().ToList();
            var data = new DataItemDetailBLL().GetList("").ToList();
            var treeList = new List<TreeGridEntity>();
            foreach (DataItemEntity item in ItemData)
            {
                TreeGridEntity tree = new TreeGridEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.ItemId) == 0 ? false : true;
                if (hasChildren == false)
                {
                    hasChildren = data.Count(t => t.ItemId == item.ItemId) == 0 ? false : true;
                }
                tree.id = item.ItemId;
                tree.text = item.ItemName;
                tree.parentId = item.ParentId;
                tree.hasChildren = hasChildren;
                tree.expanded = true;
                string itemJson = item.ToJson();
                itemJson = itemJson.Insert(1, "\"ItemDetailId\":\"" + item.ItemId + "\",");
                tree.entityJson = itemJson;
                treeList.Add(tree);
            }
            foreach (DataItemDetailEntity item in data)
            {
                TreeGridEntity tree = new TreeGridEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.ItemDetailId) == 0 ? false : true;
                tree.id = item.ItemDetailId;
                tree.text = item.ItemName;
                if (item.ParentId == "0")
                {
                    tree.parentId = item.ItemId;
                }
                else
                {
                    tree.parentId = item.ParentId;
                }
                tree.hasChildren = hasChildren;
                tree.expanded = true;
                string itemJson = item.ToJson();
                tree.entityJson = itemJson;
                treeList.Add(tree);
            }
            return Content(treeList.TreeJson());
        }
        #endregion
    }
}
